package com.open.capacity.form.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.form.dao.FormDbSourceDao;
import com.open.capacity.form.model.FormDbSource;
import com.open.capacity.form.service.FormDbSourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class FormDbSourceServiceImpl implements FormDbSourceService {

    @Autowired
    private FormDbSourceDao dbSourceDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(FormDbSource dbSource) {
        dbSource.setCreateTime(new Date());
        dbSource.setUpdateTime(dbSource.getCreateTime());

        dbSourceDao.save(dbSource);
        log.info("保存数据源：{}", dbSource);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(FormDbSource dbSource) {
        dbSource.setUpdateTime(new Date());

        dbSourceDao.updateByOps(dbSource);
        log.info("修改数据源：{}", dbSource);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        FormDbSource dbSource = dbSourceDao.findById(id);

        dbSourceDao.delete(id);

        log.info("删除数据源：{}", dbSource);
    }


    @Override
    public FormDbSource findById(String id) {
        return dbSourceDao.findById(id);
    }

    @Override
    public FormDbSource findByCode(String code) {
        Map<String, Object> params = new HashMap<>();
        params.put("code", code);
        List<FormDbSource> list = dbSourceDao.findList(params);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        } else {
            return list.get(0);
        }
    }

    @Override
    public List<FormDbSource> findDbSources(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<FormDbSource> list = dbSourceDao.findList(params);
        return list;
    }


    @Override
    public Result saveOrUpdate(FormDbSource dbSource, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            FormDbSource model = dbSourceDao.findById(keyValue);
            if (model != null) {
                dbSource.setId(keyValue);
                dbSource.setUpdateTime(new Date());
                i = dbSourceDao.updateByOps(dbSource);
            }
        } else {
            dbSource.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            dbSource.setCreateTime(new Date());
            dbSource.setUpdateTime(dbSource.getCreateTime());
            i = dbSourceDao.save(dbSource);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
